﻿#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.dll"
#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.Runtime.dll"
#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.DocumentManagement.dll"
#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.Taxonomy.dll"

#load "Client.fs"

open Client
open Microsoft.SharePoint.Client
open Microsoft.SharePoint.Client.Application
open Microsoft.SharePoint.Client.Taxonomy
open Microsoft.SharePoint.Client.WebParts
open Microsoft.SharePoint.Client.DocumentManagement
open Microsoft.SharePoint.Client.DocumentSet
open System.Security;
open System;
open System.Xml;
open System.Collections.Generic
    
// =======================================================
// Modify ContentTypes and associated metadata
// =======================================================

let web = Client.Context.Web
Client.Load web.ContentTypes
let ct = web.ContentTypes.GetById("0x0120D52000C80B6C3528CA4E45B9BC85E4AEE90E3A") 
ct.ReadOnly <- true

Client.Load web.AvailableContentTypes
for ct in web.AvailableContentTypes do 
    Client.Load ct
    let msg = ct.Name
    printfn "%s\n" msg

let delCT = web.AvailableContentTypes.GetById("0x010109")
Client.Load delCT
delCT.Hidden <- true
delCT.Update(true)
web.Update()
Client.ExecuteQuery()


let list = web.Lists.GetByTitle("Documents")
Client.Load list.ContentTypes

for ct in list.ContentTypes do
    Client.Load ct
    Client.Load ct.FieldLinks
    for fl in ct.FieldLinks do
        Client.Load fl
        let msg = ct.Name + " | " + fl.Name
        printfn "%s\n" msg
        if fl.Name = "MyContentType" then
            fl.Hidden <- false
            ct.Update(false)
            Client.ExecuteQuery()
            let msg = ct.Name + " | " + fl.Name + " | hidden"
            printfn "%s\n" msg